package com.ztesoft.web.baseconfig.service.impl;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ztesoft.core.cache.EhCacheService;
import com.ztesoft.core.convert.IArgConversionService;
import com.ztesoft.core.idproduce.ISequenceGenerator;
import com.ztesoft.framework.exception.BaseAppException;
import com.ztesoft.framework.log.ZTEsoftLogManager;
import com.ztesoft.framework.util.BeanUtils;
import com.ztesoft.framework.util.JsonUtil;
import com.ztesoft.framework.util.Utils;
import com.ztesoft.web.baseconfig.db.arg.DmPlanArg;
import com.ztesoft.web.baseconfig.db.arg.DmPlanArg.DmPlanCriteria;
import com.ztesoft.web.baseconfig.db.arg.DmStrategyArg;
import com.ztesoft.web.baseconfig.db.arg.DmStrategyArg.DmStrategyCriteria;
import com.ztesoft.web.baseconfig.db.arg.DmStrategyDataobjRelArg;
import com.ztesoft.web.baseconfig.db.arg.DmStrategyDataobjRelArg.DmStrategyDataobjRelCriteria;
import com.ztesoft.web.baseconfig.db.dao.DmPlanDao;
import com.ztesoft.web.baseconfig.db.dao.DmStrategyDao;
import com.ztesoft.web.baseconfig.db.dao.DmStrategyDataobjRelDao;
import com.ztesoft.web.baseconfig.db.po.DmDataStatePO;
import com.ztesoft.web.baseconfig.db.po.DmPlanPO;
import com.ztesoft.web.baseconfig.db.po.DmPlanRelationPO;
import com.ztesoft.web.baseconfig.db.po.DmStrategyDataobjRelPO;
import com.ztesoft.web.baseconfig.db.po.DmStrategyPO;
import com.ztesoft.web.baseconfig.db.po.TableNamePO;
import com.ztesoft.web.baseconfig.service.IDmDataStateService;
import com.ztesoft.web.baseconfig.service.IDmFilePlanService;
import com.ztesoft.web.baseconfig.service.IDmPlanRelationService;
import com.ztesoft.web.baseconfig.service.IDmStrategyDataobjRelService;
import com.ztesoft.web.baseconfig.service.IDmStrategyService;
import com.ztesoft.web.baseconfig.vo.DmFilePlanVO;
import com.ztesoft.web.baseconfig.vo.XpathAndFileNameVO;
import com.ztesoft.web.common.DMSConstant;
import com.ztesoft.web.common.DMSConstant.Status;
import com.ztesoft.web.common.db.arg.DmHostConnInfoArg;
import com.ztesoft.web.common.db.arg.DmHostConnInfoArg.DmHostConnInfoCriteria;
import com.ztesoft.web.common.db.dao.DmHostConnInfoDao;
import com.ztesoft.web.common.db.po.DmHostConnInfoPO;
import com.ztesoft.web.common.exchange.ExchangeFunc;
import com.ztesoft.web.common.vo.DmHostConnInfoVO;
import com.ztesoft.web.filecommon.db.arg.FileDirectoryDefineArg;
import com.ztesoft.web.filecommon.db.arg.FileDirectoryDefineArg.FileDirectoryDefineCriteria;
import com.ztesoft.web.filecommon.db.arg.FileObjServerRelArg;
import com.ztesoft.web.filecommon.db.arg.FileObjServerRelArg.FileObjServerRelCriteria;
import com.ztesoft.web.filecommon.db.arg.FileScanRuleInstanceArg;
import com.ztesoft.web.filecommon.db.arg.FileScanRuleInstanceArg.FileScanRuleInstanceCriteria;
import com.ztesoft.web.filecommon.db.arg.XpathVariantRelArg;
import com.ztesoft.web.filecommon.db.arg.XpathVariantRelArg.XpathVariantRelCriteria;
import com.ztesoft.web.filecommon.db.dao.FileDirectoryDefineDao;
import com.ztesoft.web.filecommon.db.dao.FileObjServerRelDao;
import com.ztesoft.web.filecommon.db.dao.FileScanRuleInstanceDao;
import com.ztesoft.web.filecommon.db.dao.XpathVariantRelDao;
import com.ztesoft.web.filecommon.db.po.DmSrcFilePO;
import com.ztesoft.web.filecommon.db.po.FileDirectoryDefinePO;
import com.ztesoft.web.filecommon.db.po.FileObjServerRelPO;
import com.ztesoft.web.filecommon.db.po.FileScanRuleInstancePO;
import com.ztesoft.web.filecommon.db.po.XpathVariantRelPO;
import com.ztesoft.web.filecommon.service.IDmSrcFileService;
import com.ztesoft.web.filecommon.service.IFileDirectoryDefineService;
import com.ztesoft.web.filecommon.service.IFileObjServerRelService;
import com.ztesoft.web.filecommon.service.IFileScanRuleInstanceService;
import com.ztesoft.web.filecommon.service.IXpathVariantRelService;

/**
 * <Description> <br>
 * 
 * @author lin.jie<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2016年6月6日 <br>
 * @since V1.0<br>
 * @see com.ztesoft.web.baseconfig.service.impl <br>
 */

@Service
public class DmFilePlanServiceImpl implements IDmFilePlanService {

	private static final ZTEsoftLogManager logger = ZTEsoftLogManager.getLogger(DmFilePlanServiceImpl.class);

	@Autowired
	private DmHostConnInfoDao dmHostConnInfoDao;
	@Autowired
	private DmPlanDao dmPlanDao;
	@Autowired
	private DmStrategyDao dmStrategyDao;
	@Autowired
	private DmStrategyDataobjRelDao dmStrategyDataobjRelDao;
	@Autowired
	private FileObjServerRelDao fileObjServerRelDao;
	@Autowired
	private FileDirectoryDefineDao fileDirectoryDefineDao;
	@Autowired
	private XpathVariantRelDao xpathVariantRelDao;
	@Autowired
	private FileScanRuleInstanceDao fileScanRuleInstanceDao;

	@Autowired
	private IDmPlanRelationService dmPlanRelationService;
	@Autowired
	private IDmStrategyService dmStrategyService;
	@Autowired
	private IDmStrategyDataobjRelService dmStrategyDataobjRelService;
	@Autowired
	private IDmSrcFileService dmSrcFileService;
	@Autowired
	private IFileObjServerRelService fileObjServerRelService;
	@Autowired
	private IFileDirectoryDefineService fileDirectoryDefineService;
	@Autowired
	private IFileScanRuleInstanceService fileScanRuleInstanceService;
	@Autowired
	private IXpathVariantRelService xpathVariantRelService;
	@Autowired
	private IDmDataStateService dmDataStateService;

	@Autowired
	private ExchangeFunc exchangeFunc;

	// 缓存内存
	@Resource(name = "frameworkEhCacheService")
	private EhCacheService dmsCacheService;

	/**
	 * 查询条件转换成Arg类的服务接口
	 */
	@Resource(name = "defaultArgConversionService")
	private IArgConversionService argConversionService;

	/**
	 * 主键生成器
	 */
	@Resource(name = "sequenceProcGenerator")
	private ISequenceGenerator sequenceGenerator;

	@Override
	public List<DmHostConnInfoVO> getHostInfo() throws BaseAppException {
		List<DmHostConnInfoVO> dhciVOList = new ArrayList<DmHostConnInfoVO>();
		DmHostConnInfoArg dhciArg = new DmHostConnInfoArg();
		DmHostConnInfoCriteria dhciCriteria = dhciArg.createCriteria();
		dhciCriteria.andStateNotEqualTo(Status.CANCELLED.toString());
		List<DmHostConnInfoPO> dhciPOList = dmHostConnInfoDao.selectByArg(dhciArg);
		for (DmHostConnInfoPO dhciPO : dhciPOList) {
			DmHostConnInfoVO dhciVO = new DmHostConnInfoVO();
			BeanUtils.copyProperties(dhciPO, dhciVO);
			dhciVO.setId(dhciPO.getHostId());
			dhciVO.setName(dhciPO.getHostName());
			dhciVOList.add(dhciVO);
		}
		return dhciVOList;
	}

	@Override
	public DmPlanPO add(DmPlanPO record) throws BaseAppException {
		logger.debug("add begin...record={0}", record);
		// 解析前台传来的数据
		try {
			// 循环类型不同，开始时间不同
			String startTime = record.getStartTime();
			if (Utils.notEmpty(record.getCycleType())) {
				if (record.getCycleType().equals("0")) {// 按月循环--2015-10-12
														// 23:00:06
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					Date startDate = sdf.parse(startTime);
					String day = startDate.getDate() < 10 ? "0" + startDate.getDate() : "" + startDate.getDate();
					if ("2016-05-01 00:00:00".equals(startTime)) {// 前台时间控件的默认值
						day = "00";
					}
					String hours = startDate.getHours() < 10 ? "0" + startDate.getHours() : "" + startDate.getHours();
					String minutes = startDate.getMinutes() < 10 ? "0" + startDate.getMinutes() : ""
							+ startDate.getMinutes();
					startTime = day + hours + minutes;
				} else if (record.getCycleType().equals("1")) {// 按日循环--23:03:06
					startTime = "00" + startTime.substring(0, 2) + startTime.substring(3, 5);
				}
			}
			record.setStartTime(startTime);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// record.setNextScheduleTime();///////////// 预计下次开始时间，还未完成
		// 源数据类型为2-文件类型，清理后分析为1-不分析
		if (record.getSourceType() == 2 || record.getOperType() == 2) {
			record.setTableAnalyseFlag(null);
			record.setIndexAnalyseFlag(null);
		}
		/*
		 * 替换，数据库必填，前台空值，的字段的值 所有非空字段：
		 * 不需要替换：dmPlanId、seqNbr、createDate、state、stateDate
		 * 需要替换：cycleType、planName
		 * 、effDate、expDate、needCheck、startTime、tableAnalyseFlag
		 * 、indexAnalyseFlag
		 */

		if (Utils.isEmpty(record.getPlanName())) {
			record.setPlanName(DMSConstant.DRAFT_STRING_REPLACE);
		}
		if (Utils.isEmpty(record.getEffDate())) {
			record.setEffDate(DMSConstant.DRAFT_DATE_REPLACE);
		}
		if (Utils.isEmpty(record.getExpDate())) {
			record.setExpDate(DMSConstant.DRAFT_DATE_REPLACE);
		} else {
			Date date = record.getExpDate();
			date.setHours(23);
			date.setMinutes(59);
			date.setSeconds(59);
			record.setExpDate(date);
		}
		// 循环类型
		if (Utils.isEmpty(record.getCycleType())) {
			record.setCycleType(DMSConstant.DRAFT_SHORT_STRING_REPLACE);
		}
		// 是否需要人工审核
		if (Utils.isEmpty(record.getNeedCheck())) {
			record.setNeedCheck(DMSConstant.DRAFT_SHORT_STRING_REPLACE);
		}
		if (Utils.isEmpty(record.getStartTime())) {
			record.setStartTime(DMSConstant.DRAFT_SHORT_STRING_REPLACE);
		}
		// if (Utils.isEmpty(record.getTableAnalyseFlag())) {
		// record.setTableAnalyseFlag(0);
		// }
		// if (Utils.isEmpty(record.getIndexAnalyseFlag())) {
		// record.setIndexAnalyseFlag(0);
		// }
		Date nowDate = new Date();
		record.setStateDate(nowDate);
		record.setSeqNbr(1);
		// 通过前台传回来的ID，从数据库里找，是否已经有这条数据了
		DmPlanPO dpPO = dmPlanDao.selectByPrimaryKey(record.getDmPlanId());
		if (Utils.isEmpty(dpPO)) {// 不存在这条记录，说明是新增
			// 查找是否存在相同计划名称
			DmPlanArg arg = new DmPlanArg();
			DmPlanCriteria criteria = arg.createCriteria();
			criteria.andPlanNameEqualTo(record.getPlanName());
			criteria.andStateNotEqualTo(Status.CANCELLED.toString());
			List<DmPlanPO> dpPOList = dmPlanDao.selectByArg(arg);
			if (dpPOList.size() != 0) {// 存在相同名称的记录，不可以保存
				record.setDmPlanId(0);
			} else {
				record.setCreateDate(nowDate);
				dmPlanDao.insertSelective(record);
			}
		} else {
			if (dpPO.getState().equals(Status.DRAFT.toString())) {// 草稿箱，点击保存草稿或者完成提交
				// 草稿箱状态为00D或者0DA，直接修改草稿箱
				dmPlanDao.updateByPrimaryKeySelective(record);
			} else {// 修改，点击提交
				// 先把原来的记录状态改成00X
				dpPO.setState(Status.CANCELLED.toString());
				dpPO.setStateDate(nowDate);
				dmPlanDao.updateByPrimaryKeySelective(dpPO);
				// 再新增一条序号为 +1 的记录
				record.setSeqNbr(dpPO.getSeqNbr() + 1);
				record.setCreateDate(dpPO.getCreateDate());
				dmPlanDao.insertSelective(record);
			}
		}
		return record;
	}

	@Override
	public DmFilePlanVO addAllInfo(DmFilePlanVO record) throws BaseAppException {
		Date nowDate = new Date();
		int dmPlanId = 0;
		int dmStrategyGroupNum = 0;
		int dmStrategyId = 0;
		int sourceDataId = 0;
		String state = Status.SUCCESS.toString();
		boolean isNeedTarget = true;
		if (record.getOperType() == 0) {
			isNeedTarget = true;
		} else {
			isNeedTarget = false;
		}
		if ("addDraft".equals(record.getAct()) || "draft".equals(record.getAct())) {
			state = Status.DRAFT.toString();
		}
		if ("edit".equals(record.getAct()) || "draft".equals(record.getAct()) || "saveDraft".equals(record.getAct())) {// 编辑、草稿箱状态
			dmPlanId = record.getDmPlanId();
			dmStrategyGroupNum = record.getGroupNbr();
			dmStrategyId = record.getDmStrategyId();
			sourceDataId = record.getSourceDataId();
		} else {
			// 主键生成器
			// 获取计划ID
			dmPlanId = sequenceGenerator.sequenceIntValue("DM_PLAN", "DM_PLAN_ID");
			// 获取策略组序号
			dmStrategyGroupNum = sequenceGenerator.sequenceIntValue("DM_STRATEGY", "DM_STRATEGY_ID");
			// 获取策略ID
			dmStrategyId = sequenceGenerator.sequenceIntValue("DM_STRATEGY", "DM_STRATEGY_ID");
			// 获取源数据对象ID
			sourceDataId = sequenceGenerator.sequenceIntValue("SOURCE_DATA", "SOURCE_DATA_ID");
		}

		// 1、添加计划
		DmPlanPO dpPO = new DmPlanPO();
		BeanUtils.copyProperties(record, dpPO);
		dpPO.setDmPlanId(dmPlanId);
		dpPO.setState(state);
		dpPO.setStateDate(nowDate);
		dpPO.setCreateDate(nowDate);
		logger.debug("添加计划信息：" + dpPO);
		dpPO = add(dpPO);

		// 2、添加计划与策略关系
		DmPlanRelationPO dprPO = new DmPlanRelationPO();
		dprPO.setDmPlanId(dmPlanId);
		dprPO.setSeqNbr(1);
		dprPO.setDmStrategyId(dmStrategyId);
		dprPO.setGroupNbr(dmStrategyGroupNum);
		dprPO.setExpDate(record.getExpDate());
		dprPO.setState(state);
		dprPO.setStateDate(nowDate);
		dprPO.setStaffId(record.getStaffId());
		logger.debug("添加计划与策略关系信息：" + dprPO);
		dmPlanRelationService.add(dprPO);

		// 3、添加策略
		DmStrategyPO dsPO = new DmStrategyPO();
		dsPO.setDmStrategyId(dmStrategyId);
		dsPO.setDmPlanId(dmPlanId);
		dsPO.setSeqNbr(1);
		dsPO.setStrategyName(record.getStrategyName());
		dsPO.setConcurrentNumber(record.getConcurrentNumber());
		dsPO.setState(state);
		dsPO.setStaffId(record.getStaffId());
		logger.debug("添加策略信息：" + dsPO);
		dmStrategyService.add(dsPO);

		// 4、策略关联源数据表
		DmStrategyDataobjRelPO dsdrPO = new DmStrategyDataobjRelPO();
		dsdrPO.setDmStrategyId(dmStrategyId);
		dsdrPO.setSourceDataId(sourceDataId);
		dsdrPO.setObjectType(2);
		dsdrPO.setMainFlag(2);
		dsdrPO.setPriority(1);
		dsdrPO.setState(state);
		dsdrPO.setStateDate(nowDate);
		logger.debug("添加策略关联源数据表信息：" + dsdrPO);
		dmStrategyDataobjRelService.add(dsdrPO);

		// 5、源数据
		DmSrcFilePO dsfPO = new DmSrcFilePO();
		dsfPO.setSourceDataId(sourceDataId);
		if (isNeedTarget) {
			dsfPO.setDmBackupType(record.getDmBackupType());
		} else {
			dsfPO.setDmBackupType("0");
		}
		dsfPO.setDmStrategyId(dmStrategyId);
		// dsfPO.setScriptPath("自定义脚本路径");
		dsfPO.setConcurrencyThreadNum(record.getConcurrentNumber());
		dsfPO.setStaffId(record.getStaffId());
		dsfPO.setState(state);
		dsfPO.setStateDate(nowDate);
		dsfPO.setCreateDate(nowDate);
		logger.debug("添加源数据-文件-信息：" + dsdrPO);
		dmSrcFileService.add(dsfPO);

		// 6、文件对象主机关联表
		if ("edit".equals(record.getAct()) || "draft".equals(record.getAct()) || "saveDraft".equals(record.getAct())) {// 编辑状态
			// 先删除原来的
			FileObjServerRelArg fosrArg = new FileObjServerRelArg();
			FileObjServerRelCriteria fosrCriteria = fosrArg.createCriteria();
			fosrCriteria.andStateNotEqualTo(Status.CANCELLED.toString());
			fosrCriteria.andObjIdEqualTo(record.getSourceDataId());
			logger.debug("删除文件对象主机关联表-文件-信息：" + fosrArg);
			fileObjServerRelDao.deleteByArg(fosrArg);
		}
		List<DmHostConnInfoVO> srcHostGridData = JsonUtil
				.toList(record.getSrcHostGridDataStr(), DmHostConnInfoVO.class);
		// 如果是同主机就直接用源主机信息
		List<DmHostConnInfoVO> destHostGridData = new ArrayList<DmHostConnInfoVO>();
		if ("1".equals(record.getDmBackupType())) {// 跨主机
			destHostGridData = JsonUtil.toList(record.getDestHostGridDataStr(), DmHostConnInfoVO.class);
		}
		for (DmHostConnInfoVO dhciVO : srcHostGridData) {
			FileObjServerRelPO fosrPO = new FileObjServerRelPO();
			int relationshipsId = sequenceGenerator.sequenceIntValue("FILE_OBJ_SERVER_REL", "RELATIONSHIPS_ID");
			fosrPO.setRelationshipsId(relationshipsId);
			fosrPO.setSrcHostId(dhciVO.getHostId());
			fosrPO.setSrcIp(dhciVO.getIp());
			if ("1".equals(record.getDmBackupType())) {// 跨主机
				fosrPO.setDestHostId(destHostGridData.get(0).getHostId());
				fosrPO.setDestIp(destHostGridData.get(0).getIp());
			} else {// 同主机
				fosrPO.setDestHostId(dhciVO.getHostId());
				fosrPO.setDestIp(dhciVO.getIp());
			}
			fosrPO.setObjId(sourceDataId);
			fosrPO.setState(state);
			fosrPO.setStateDate(nowDate);
			fosrPO.setCreateDate(nowDate);
			logger.debug("添加文件对象主机关联表-信息：" + dsdrPO);
			fileObjServerRelService.add(fosrPO);
		}

		// 7、目录表达式
		int seq_fdd = 1;
		Date createDate_fdd = new Date();
		int seq_xvr = 1;
		Date createDate_xvr = new Date();
		if ("edit".equals(record.getAct()) || "draft".equals(record.getAct()) || "saveDraft".equals(record.getAct())) {// 编辑状态
			// 先删除原来的---目录表达式、表达式变量映射关系表
			// 目录表达式
			FileDirectoryDefineArg fddArg = new FileDirectoryDefineArg();
			FileDirectoryDefineCriteria fddCriteria = fddArg.createCriteria();
			fddCriteria.andStateNotEqualTo(Status.CANCELLED.toString());
			fddCriteria.andSourceDataIdEqualTo(record.getSourceDataId());
			FileDirectoryDefinePO fddPO = fileDirectoryDefineDao.selectByArg(fddArg).get(0);
			seq_fdd = fddPO.getSeq() + 1;
			createDate_fdd = fddPO.getCreateDate();
			logger.debug("删除目录表达式-信息：" + fddArg);
			fileDirectoryDefineDao.deleteByArg(fddArg);

			// 表达式变量映射关系表--不包括文件名规则的变量
			XpathVariantRelArg xvrArg = new XpathVariantRelArg();
			XpathVariantRelCriteria xvrCriteria = xvrArg.createCriteria();
			xvrCriteria.andStateNotEqualTo(Status.CANCELLED.toString());
			xvrCriteria.andSourceDataIdEqualTo(record.getSourceDataId());
			xvrCriteria.andDataTypeNotEqualTo(2);// 不包括文件名规则的变量
			List<XpathVariantRelPO> xvrPOList = xpathVariantRelDao.selectByArg(xvrArg);
			if (xvrPOList.size() != 0) {
				seq_xvr = xvrPOList.get(0).getSeq() + 1;
				createDate_xvr = xvrPOList.get(0).getCreateDate();
			}
			logger.debug("删除表达式变量映射关系表-信息：" + xvrArg);
			xpathVariantRelDao.deleteByArg(xvrArg);
		}
		List<XpathAndFileNameVO> sourceXpathGridData = JsonUtil.toList(record.getSourceXpathGridDataStr(),
				XpathAndFileNameVO.class);
		int dirOrder_src = 1;
		int dirOrder_target = 1;
		for (int i = 0; i < sourceXpathGridData.size(); i++) {
			FileDirectoryDefinePO fddPO = new FileDirectoryDefinePO();
			int directoryId = sequenceGenerator.sequenceIntValue("FILE_DIRECTORY_DEFINE", "DIRECTORY_ID");
			fddPO.setDirectoryId(directoryId);
			fddPO.setSourceDataId(sourceDataId);
			fddPO.setDirOrder(i + 1);
			fddPO.setSeq(seq_fdd);
			fddPO.setStaffId(record.getStaffId());
			fddPO.setState(state);
			fddPO.setStateDate(nowDate);
			fddPO.setCreateDate(createDate_fdd);
			// 源目录表达式
			StringBuffer sourceXpath = new StringBuffer("/");
			List<TableNamePO> sourceXpathList = JsonUtil.toList(sourceXpathGridData.get(i).getXpathStr(),
					TableNamePO.class);
			int priority_src = 1;
			for (TableNamePO tnPO : sourceXpathList) {
				if ("directory".equals(tnPO.getType())) {// 不需要保存
					continue;
				} else if ("timer".equals(tnPO.getType())) {// 时间变量
					String[] timerStrs = tnPO.getValue().split(",");
					sourceXpath.append("${" + priority_src + "}");
					// 9、表达式变量映射关系表
					setXpathVariantRelPO(sourceDataId, 0, dirOrder_src, priority_src, Integer.parseInt(timerStrs[0]),
							timerStrs[1] + "," + timerStrs[2], state, seq_xvr, createDate_xvr);
					priority_src++;
				} else if ("list".equals(tnPO.getType())) {// 可转换变量
					sourceXpath.append("${" + priority_src + "}");
					// 9、表达式变量映射关系表
					setXpathVariantRelPO(sourceDataId, 0, dirOrder_src, priority_src,
							Integer.parseInt(tnPO.getValue()), null, state, seq_xvr, createDate_xvr);
					priority_src++;
				} else if ("4".equals(tnPO.getType())) {// 账期
					sourceXpath.append("${" + priority_src + "}");
					// 9、表达式变量映射关系表
					setXpathVariantRelPO(sourceDataId, 0, dirOrder_src, priority_src, 4, tnPO.getValue(), state,
							seq_xvr, createDate_xvr);
					priority_src++;
				} else {
					sourceXpath.append(tnPO.getValue());
				}
			}
			String sourceXpathStr = sourceXpath.toString() + "/";
			sourceXpathStr = sourceXpathStr.replaceAll("//", "/");
			sourceXpathStr = sourceXpathStr.replaceAll("//", "/");
			fddPO.setSourceXpath(sourceXpathStr);

			if (isNeedTarget) {
				List<XpathAndFileNameVO> targetXpathGridData = JsonUtil.toList(record.getTargetXpathGridDataStr(),
						XpathAndFileNameVO.class);
				// 目标目录表达式
				StringBuffer targetXpath = new StringBuffer("/");
				List<TableNamePO> targetXpathList = new ArrayList<TableNamePO>();
				if (targetXpathGridData.size() == 1) {
					targetXpathList = JsonUtil.toList(targetXpathGridData.get(0).getXpathStr(), TableNamePO.class);
				} else {
					targetXpathList = JsonUtil.toList(targetXpathGridData.get(i).getXpathStr(), TableNamePO.class);
				}
				int priority_target = 1;
				for (TableNamePO tnPO : targetXpathList) {
					if ("directory".equals(tnPO.getType())) {// 不需要保存
						continue;
					} else if ("timer".equals(tnPO.getType())) {// 时间变量
						String[] timerStrs = tnPO.getValue().split(",");
						targetXpath.append("${" + priority_target + "}");
						// 9、表达式变量映射关系表
						setXpathVariantRelPO(sourceDataId, 1, dirOrder_target, priority_target,
								Integer.parseInt(timerStrs[0]), timerStrs[1] + "," + timerStrs[2], state, seq_xvr,
								createDate_xvr);
						priority_target++;
					} else if ("list".equals(tnPO.getType())) {// 可转换变量
						targetXpath.append("${" + priority_target + "}");
						// 9、表达式变量映射关系表
						setXpathVariantRelPO(sourceDataId, 1, dirOrder_target, priority_target,
								Integer.parseInt(tnPO.getValue()), null, state, seq_xvr, createDate_xvr);
						priority_target++;
					} else if ("4".equals(tnPO.getType())) {// 账期
						targetXpath.append("${" + priority_target + "}");
						// 9、表达式变量映射关系表
						setXpathVariantRelPO(sourceDataId, 1, dirOrder_target, priority_target, 4, tnPO.getValue(),
								state, seq_xvr, createDate_xvr);
						priority_target++;
					} else {
						targetXpath.append(tnPO.getValue());
					}
				}
				String targetXpathStr = targetXpath.toString() + "/";
				targetXpathStr = targetXpathStr.replaceAll("//", "/");
				targetXpathStr = targetXpathStr.replaceAll("//", "/");
				fddPO.setTargetXpath(targetXpathStr);
			}
			logger.debug("添加目录表达式-信息：" + fddPO);
			fileDirectoryDefineService.add(fddPO);

			dirOrder_src++;
			dirOrder_target++;
		}

		// 8、文件检索规则实例
		List<XpathAndFileNameVO> fileNameGridData = new ArrayList<XpathAndFileNameVO>();
		List<Integer> dirOrders = new ArrayList<Integer>();
		List<Integer> dirOrders_delete = new ArrayList<Integer>();
		if ("edit".equals(record.getAct()) || "draft".equals(record.getAct()) || "saveDraft".equals(record.getAct())) {// 编辑状态
			// 先过滤掉没有变动的规则
			List<XpathAndFileNameVO> fileNameGridData_original = JsonUtil.toList(record.getFileNameGridDataStr(),
					XpathAndFileNameVO.class);
			for (int i = 0; i < fileNameGridData_original.size(); i++) {
				if (fileNameGridData_original.get(i).getDirOrder() == 0) {// 新增的配置规则
					fileNameGridData.add(fileNameGridData_original.get(i));
					continue;
				} else if (fileNameGridData_original.get(i).getIsDelete() == 1) {// 删除的配置规则
					dirOrders_delete.add(fileNameGridData_original.get(i).getDirOrder());
					continue;
				} else {// 没有变动的规则
					dirOrders.add(fileNameGridData_original.get(i).getDirOrder());
				}
			}
			// 把没有变动的规则前缀改掉，还有fileType
			FileScanRuleInstanceArg fsriArg_updata = new FileScanRuleInstanceArg();
			FileScanRuleInstanceCriteria fsriCriteria_updata = fsriArg_updata.createCriteria();
			fsriCriteria_updata.andStateNotEqualTo(Status.CANCELLED.toString());
			if (dirOrders.size() != 0) {
				fsriCriteria_updata.andDirOrderIn(dirOrders);
			}
			fsriCriteria_updata.andSourceDataIdEqualTo(dsdrPO.getSourceDataId());
			List<FileScanRuleInstancePO> fsriPOList = fileScanRuleInstanceDao.selectByArg(fsriArg_updata);
			for (FileScanRuleInstancePO fsriPO : fsriPOList) {
				// 查找出原来是什么文件类型，去除前面的字符串“-type f”之类的
				String fileName = fsriPO.getFilenameScanXpath();
				if (!"0".equals(fsriPO.getFileType())) {// 文件或者目录
					fileName = fileName.substring(8, fileName.length());
				}
				// 再判断现在是什么文件类型
				if ("1".equals(record.getFileType())) {// 目录
					fileName = "-type d " + fileName;
					fsriPO.setFileType("1");
				} else if ("2".equals(record.getFileType())) {// 文件
					fileName = "-type f " + fileName;
					fsriPO.setFileType("2");
				}else{
					fsriPO.setFileType("0");
				}
				fsriPO.setFilenameScanXpath(fileName);
				fileScanRuleInstanceDao.updateByPrimaryKeySelective(fsriPO);
			}
			
			// 先删除原来的--不包括没有变动的规则
			// 文件检索规则实例
			FileScanRuleInstanceArg fsriArg = new FileScanRuleInstanceArg();
			FileScanRuleInstanceCriteria fsriCriteria = fsriArg.createCriteria();
			fsriCriteria.andStateNotEqualTo(Status.CANCELLED.toString());
			if (dirOrders.size() != 0) {
				fsriCriteria.andDirOrderNotIn(dirOrders);
			}
			if (dirOrders_delete.size() != 0) {
				fsriCriteria.andDirOrderIn(dirOrders_delete);
			}
			fsriCriteria.andSourceDataIdEqualTo(dsdrPO.getSourceDataId());
			logger.debug("删除文件检索规则实例-信息：" + fsriArg);
			fileScanRuleInstanceDao.deleteByArg(fsriArg);

			// 表达式变量映射关系表
			XpathVariantRelArg xvrArg = new XpathVariantRelArg();
			XpathVariantRelCriteria xvrCriteria = xvrArg.createCriteria();
			xvrCriteria.andStateNotEqualTo(Status.CANCELLED.toString());
			xvrCriteria.andSourceDataIdEqualTo(record.getSourceDataId());
			if (dirOrders.size() != 0) {
				xvrCriteria.andDirOrderNotIn(dirOrders);
			}
			xvrCriteria.andDataTypeEqualTo(2);
			List<XpathVariantRelPO> xvrPOList = xpathVariantRelDao.selectByArg(xvrArg);
			if (xvrPOList.size() != 0) {
				seq_xvr = xvrPOList.get(0).getSeq() + 1;
				createDate_xvr = xvrPOList.get(0).getCreateDate();
			}
			logger.debug("删除表达式变量映射关系表-信息：" + xvrArg);
			xpathVariantRelDao.deleteByArg(xvrArg);

			// 如果是草稿箱保存
			if ("saveDraft".equals(record.getAct())) {
				// 把原来不变的改成00A
				// 文件检索规则实例
				FileScanRuleInstanceArg fsriArg_saveDraft = new FileScanRuleInstanceArg();
				FileScanRuleInstanceCriteria fsriCriteria_saveDraft = fsriArg_saveDraft.createCriteria();
				fsriCriteria_saveDraft.andStateEqualTo(Status.DRAFT.toString());
				if (dirOrders.size() != 0) {
					fsriCriteria_saveDraft.andDirOrderIn(dirOrders);
				}
				fsriCriteria_saveDraft.andSourceDataIdEqualTo(dsdrPO.getSourceDataId());
				logger.debug("将文件检索规则实例状态改为00A-信息：" + fsriArg_saveDraft);
				fileScanRuleInstanceDao.updataTo00A(fsriArg_saveDraft);

				// 表达式变量映射关系表
				XpathVariantRelArg xvrArg_saveDraft = new XpathVariantRelArg();
				XpathVariantRelCriteria xvrCriteria_saveDraft = xvrArg_saveDraft.createCriteria();
				xvrCriteria_saveDraft.andStateEqualTo(Status.DRAFT.toString());
				xvrCriteria_saveDraft.andSourceDataIdEqualTo(record.getSourceDataId());
				if (dirOrders.size() != 0) {
					xvrCriteria_saveDraft.andDirOrderIn(dirOrders);
				}
				xvrCriteria_saveDraft.andDataTypeEqualTo(2);
				logger.debug("将表达式变量映射关系表状态改为00A-信息：" + xvrArg_saveDraft);
				xpathVariantRelDao.updataTo00A(xvrArg_saveDraft);
			}
		} else {
			fileNameGridData = JsonUtil.toList(record.getFileNameGridDataStr(), XpathAndFileNameVO.class);
		}
		int dirOrder_fileName = 1;
		if (dirOrders.size() != 0) {
			dirOrder_fileName = dirOrders.get(dirOrders.size() - 1) + 1;
		}
		String headStr = "-type f ";
		if("1".equals(record.getFileType())){// 目录
			headStr = "-type d ";
		}else if("0".equals(record.getFileType())){// 不限
			headStr = " ";
		}
		for (int i = 0; i < fileNameGridData.size(); i++) {
			StringBuffer filenameScanXpath = new StringBuffer(headStr);
			StringBuffer scanStatements = new StringBuffer("scanStatements");// ----------------还没做
			List<TableNamePO> fileNameList = JsonUtil.toList(fileNameGridData.get(i).getFileNameStr(),
					TableNamePO.class);
			int priority_fileName = 1;
			for (TableNamePO tnPO : fileNameList) {// -type f ' -name 'sssss'
				if ("contain".equals(tnPO.getType())) {// 包含
					filenameScanXpath.append("' ");
					filenameScanXpath.append("-name '");
					continue;
				} else if ("noContain".equals(tnPO.getType())) {// 不包含
					filenameScanXpath.append("' ");
					filenameScanXpath.append("! -name '");
					continue;
				} else if ("timer".equals(tnPO.getType())) {// 时间变量
					String[] timerStrs = tnPO.getValue().split(",");
					filenameScanXpath.append("${" + priority_fileName + "}");
					// 9、表达式变量映射关系表
					setXpathVariantRelPO(sourceDataId, 2, dirOrder_fileName, priority_fileName,
							Integer.parseInt(timerStrs[0]), timerStrs[1] + "," + timerStrs[2], state, seq_xvr,
							createDate_xvr);
					priority_fileName++;
				} else if ("list".equals(tnPO.getType())) {// 可转换变量
					filenameScanXpath.append("${" + priority_fileName + "}");
					// 9、表达式变量映射关系表
					setXpathVariantRelPO(sourceDataId, 2, dirOrder_fileName, priority_fileName,
							Integer.parseInt(tnPO.getValue()), null, state, seq_xvr, createDate_xvr);
					priority_fileName++;
				} else if ("4".equals(tnPO.getType())) {// 账期
					filenameScanXpath.append("${" + priority_fileName + "}");
					// 9、表达式变量映射关系表
					setXpathVariantRelPO(sourceDataId, 2, dirOrder_fileName, priority_fileName, 4, tnPO.getValue(),
							state, seq_xvr, createDate_xvr);
					priority_fileName++;
				} else if ("nowDate".equals(tnPO.getType())) {// 当前年月日
					filenameScanXpath.append("${" + priority_fileName + "}");
					// 9、表达式变量映射关系表
					setXpathVariantRelPO(sourceDataId, 2, dirOrder_fileName, priority_fileName, 0, "nowDate", state,
							seq_xvr, createDate_xvr);
					priority_fileName++;
				} else if ("nowDateWithSplit".equals(tnPO.getType())) {// 当前年-月-日
					filenameScanXpath.append("${" + priority_fileName + "}");
					// 9、表达式变量映射关系表
					setXpathVariantRelPO(sourceDataId, 2, dirOrder_fileName, priority_fileName, 8, "nowDateWithSplit", state,
							seq_xvr, createDate_xvr);
					priority_fileName++;
				}else {
					filenameScanXpath.append(tnPO.getValue());
				}
			}
			filenameScanXpath.append("'");
			FileScanRuleInstancePO fsriPO = new FileScanRuleInstancePO();
			int scanRuleId = sequenceGenerator.sequenceIntValue("FILE_SCAN_RULE_INSTANCE", "SCAN_RULE_ID");
			fsriPO.setScanRuleId(scanRuleId);
			fsriPO.setSourceDataId(sourceDataId);
			fsriPO.setDirOrder(dirOrder_fileName);
			fsriPO.setScanRange(record.getScanRange());
			fsriPO.setFileType(record.getFileType());
			fsriPO.setFilesizeMin(0);
			fsriPO.setFilesizeMax(0);
			if ("0".equals(record.getFileType())) {// 不限
				fsriPO.setFilenameScanXpath(filenameScanXpath.toString().substring(0, 1)
						+ filenameScanXpath.toString().substring(3, filenameScanXpath.length()));
			} else {
				fsriPO.setFilenameScanXpath(filenameScanXpath.toString().substring(0, 8)
						+ filenameScanXpath.toString().substring(10, filenameScanXpath.length()));
			}
			fsriPO.setScanStatements(scanStatements.toString());// ----------------还没做
			fsriPO.setSourceFileZipSwitch(record.getSourceFileZipSwitch());
			fsriPO.setTargetFileZipSwitch(record.getTargetFileZipSwitch());
			fsriPO.setRuleDesc(record.getRuleDesc());
			fsriPO.setState(state);
			fsriPO.setStateDate(nowDate);
			fsriPO.setCreateDate(nowDate);
			logger.debug("添加文件检索规则-信息：" + fsriPO);
			fileScanRuleInstanceService.add(fsriPO);

			dirOrder_fileName++;
		}

		// 数据恢复：存人数据库
		DmDataStatePO ddsPO = new DmDataStatePO();
		ddsPO.setDmPlanId(dmPlanId);
		ddsPO.setDmTaskId(-999);
		ddsPO.setState(0);
		ddsPO.setStateDate(new Date());
		ddsPO.setCreateDate(new Date());
		logger.debug("数据恢复状态-表-初始化：" + ddsPO);
		dmDataStateService.add(ddsPO);

		// 数据恢复：存人内存
		logger.debug("数据恢复状态-内存-初始化：dataRecovery_" + dmPlanId);
		dmsCacheService.put("dmsCache", "dataRecovery_" + dmPlanId, 0);

		return record;
	}

	/**
	 * 设置并添加-表达式变量映射关系表
	 * 
	 * @return
	 * @throws BaseAppException
	 */
	private XpathVariantRelPO setXpathVariantRelPO(Integer sourceDataId, Integer dataType, Integer dirOrder,
			Integer priority, Integer funcId, String variantDefineValue, String state, Integer seq, Date createDate)
			throws BaseAppException {
		Date nowDate = new Date();
		XpathVariantRelPO xvrPO = new XpathVariantRelPO();
		xvrPO.setSourceDataId(sourceDataId);
		xvrPO.setDataType(dataType);
		xvrPO.setDirOrder(dirOrder);
		xvrPO.setParamPriority(priority);
		xvrPO.setSeq(seq);
		xvrPO.setVariantDefineType(1);
		if (variantDefineValue != null) {
			xvrPO.setVariantDefineValue(variantDefineValue);
		}
		xvrPO.setFuncId(funcId);
		xvrPO.setFuncType(1);
		xvrPO.setState(state);
		xvrPO.setStateDate(nowDate);
		xvrPO.setCreateDate(createDate);
		logger.debug("添加表达式变量映射关系-信息：" + xvrPO);
		xpathVariantRelService.add(xvrPO);
		return xvrPO;
	}

	@Override
	public boolean planNameIsRepeated(DmFilePlanVO record) throws BaseAppException {
		DmPlanArg dpArg = new DmPlanArg();
		DmPlanCriteria dpCriteria = dpArg.createCriteria();
		dpCriteria.andStateNotEqualTo(Status.CANCELLED.toString());
		dpCriteria.andPlanNameEqualTo(record.getPlanName());
		List<DmPlanPO> dpPOList = dmPlanDao.selectByArg(dpArg);
		if (dpPOList.size() != 0) {// 计划名称重复
			if (dpPOList.get(0).getDmPlanId().equals(record.getDmPlanId())) {// 编辑
				return false;
			} else {
				return true;
			}
		} else {
			return false;
		}
	}

	@Override
	public boolean strategyNameIsRepeated(DmFilePlanVO record) throws BaseAppException {
		DmStrategyArg dsArg = new DmStrategyArg();
		DmStrategyCriteria dsCriteria = dsArg.createCriteria();
		dsCriteria.andStateNotEqualTo(Status.CANCELLED.toString());
		dsCriteria.andStrategyNameEqualTo(record.getStrategyName());
		List<DmStrategyPO> dsPOList = dmStrategyDao.selectByArg(dsArg);
		if (dsPOList.size() != 0 && !record.getDmStrategyId().equals(dsPOList.get(0).getDmStrategyId())) {// 策略名称重复
			return true;
		} else {
			return false;
		}
	}

	@Override
	public DmFilePlanVO getPlanInfo(DmFilePlanVO record) throws BaseAppException {
		// 获取计划信息
		DmPlanArg dpArg = new DmPlanArg();
		DmPlanCriteria dpCriteria = dpArg.createCriteria();
		dpCriteria.andDmPlanIdEqualTo(record.getDmPlanId());
		dpCriteria.andStateNotEqualTo(Status.CANCELLED.toString());
		List<DmPlanPO> dpPOList = dmPlanDao.selectByArg(dpArg);
		if (dpPOList.size() != 0) {
			BeanUtils.copyProperties(dpPOList.get(0), record);
		}
		// 获取策略信息
		List<DmStrategyPO> dsPOList = dmStrategyService.selectByPlanId(record.getDmPlanId());
		if (dsPOList.size() != 0) {
			record.setDmStrategyPO(dsPOList.get(0));
			// 获取文件检索规则实例信息-为了源和目标是否压缩
			List<FileScanRuleInstancePO> fsriPOList = fileScanRuleInstanceService.selectByDmStrategyId(dsPOList.get(0)
					.getDmStrategyId());
			if (fsriPOList.size() != 0) {
				record.setSourceFileZipSwitch(fsriPOList.get(0).getSourceFileZipSwitch());
				record.setTargetFileZipSwitch(fsriPOList.get(0).getTargetFileZipSwitch());
			}
		}
		return record;
	}

	@Override
	public DmFilePlanVO getStrategyInfo(DmFilePlanVO record) throws BaseAppException {
		// 获取计划信息，判断是否有备份环节，是否需要目标数据
		DmPlanPO dpPO = dmPlanDao.selectByDmStrategyId(record.getDmStrategyId());
		boolean isNeedTarget = true;
		if (dpPO.getOperType() == 0) {// 体检备份清理，需要目标数据
			isNeedTarget = true;
		} else {
			isNeedTarget = false;
		}

		DmStrategyDataobjRelArg dsdrArg = new DmStrategyDataobjRelArg();
		DmStrategyDataobjRelCriteria dsdrCriteria = dsdrArg.createCriteria();
		dsdrCriteria.andDmStrategyIdEqualTo(record.getDmStrategyId());
		dsdrCriteria.andStateNotEqualTo(Status.CANCELLED.toString());
		DmStrategyDataobjRelPO dsdrPO = dmStrategyDataobjRelDao.selectByArg(dsdrArg).get(0);
		logger.debug("策略与数据关系：" + dsdrPO);

		// 源文件对象信息
		DmSrcFilePO dsfPO = dmSrcFileService.selectByPrimaryKey(dsdrPO.getSourceDataId());
		BeanUtils.copyProperties(dsfPO, record);
		record.setConcurrentNumber(dsfPO.getConcurrencyThreadNum());// 并发数
		logger.debug("源文件对象信息：" + dsfPO);

		// 主机关联对象信息
		FileObjServerRelArg fosrArg = new FileObjServerRelArg();
		FileObjServerRelCriteria fosrCriteria = fosrArg.createCriteria();
		fosrCriteria.andStateNotEqualTo(Status.CANCELLED.toString());
		fosrCriteria.andObjIdEqualTo(dsdrPO.getSourceDataId());
		List<FileObjServerRelPO> fosrPOList = fileObjServerRelDao.selectByArg(fosrArg);
		logger.debug("主机关联对象信息：" + fosrPOList);
		// 主机对象信息
		List<DmHostConnInfoVO> dhciVOList = getHostInfo();
		// 源主机
		StringBuffer srcHostGridDataStr = new StringBuffer("[");
		for (FileObjServerRelPO fosrPO : fosrPOList) {
			for (DmHostConnInfoVO dhciVO : dhciVOList) {
				if (dhciVO.getHostId().equals(fosrPO.getSrcHostId())) {
					srcHostGridDataStr.append("{\"busiSys\":\"" + dhciVO.getBusiSys() + "\",");
					srcHostGridDataStr.append("\"hostName\":\"" + dhciVO.getHostName() + "\",");
					break;
				}
			}
			srcHostGridDataStr.append("\"hostId\":" + fosrPO.getSrcHostId() + ",");
			srcHostGridDataStr.append("\"ip\":\"" + fosrPO.getSrcIp() + "\"},");
		}
		srcHostGridDataStr.deleteCharAt(srcHostGridDataStr.length() - 1);
		srcHostGridDataStr.append("]");
		record.setSrcHostGridDataStr(srcHostGridDataStr.toString());
		// 目标主机
		if (isNeedTarget) {
			StringBuffer destHostGridDataStr = new StringBuffer("{");
			for (DmHostConnInfoVO dhciVO : dhciVOList) {
				if (dhciVO.getHostId().equals(fosrPOList.get(0).getDestHostId())) {
					destHostGridDataStr.append("\"busiSys\":\"" + dhciVO.getBusiSys() + "\",");
					destHostGridDataStr.append("\"hostName\":\"" + dhciVO.getHostName() + "\",");
					break;
				}
			}
			destHostGridDataStr.append("\"hostId\":" + fosrPOList.get(0).getDestHostId() + ",\"ip\":\""
					+ fosrPOList.get(0).getDestIp() + "\"}");
			record.setDestHostGridDataStr(destHostGridDataStr.toString());
		}

		// 目录表达式信息
		FileDirectoryDefineArg fddArg = new FileDirectoryDefineArg();
		FileDirectoryDefineCriteria fddCriteria = fddArg.createCriteria();
		fddCriteria.andStateNotEqualTo(Status.CANCELLED.toString());
		fddCriteria.andSourceDataIdEqualTo(dsdrPO.getSourceDataId());
		fddArg.setOrderByClause("DIR_ORDER");
		List<FileDirectoryDefinePO> fddPOList = fileDirectoryDefineDao.selectByArg(fddArg);
		logger.debug("目录表达式信息：" + fddPOList);
		// 表达式变量映射信息
		XpathVariantRelArg xvrArg = new XpathVariantRelArg();
		XpathVariantRelCriteria xvrCriteria = xvrArg.createCriteria();
		xvrCriteria.andStateNotEqualTo(Status.CANCELLED.toString());
		xvrCriteria.andSourceDataIdEqualTo(dsdrPO.getSourceDataId());
		xvrArg.setOrderByClause("DATA_TYPE, DIR_ORDER, PARAM_PRIORITY");
		List<XpathVariantRelPO> xvrPOList = xpathVariantRelDao.selectByArg(xvrArg);
		logger.debug("表达式变量映射信息：" + xvrPOList);
		// 源目录
		List<XpathAndFileNameVO> sourceXpathGridDataList = new ArrayList<XpathAndFileNameVO>();
		// 目标目录
		List<XpathAndFileNameVO> targetXpathGridDataList = new ArrayList<XpathAndFileNameVO>();
		for (int i = 0; i < fddPOList.size(); i++) {
			// 源目录
			XpathAndFileNameVO sourceXpathGridData = new XpathAndFileNameVO();
			StringBuffer sourceXpathStr = new StringBuffer("[");
			sourceXpathStr.append("{\"type\":\"slash\",\"value\":\"/\"},");// 前台从1开始展示，这边要多一个
			String[] sourceXpaths = fddPOList.get(i).getSourceXpath().split("/");
			for (int j = 1; j < sourceXpaths.length; j++) {
				sourceXpathStr.append("{\"type\":\"slash\",\"value\":\"/\"},");
				if (sourceXpaths[j].contains("${")) {// 有变量
					separateVariable(sourceXpaths[j], sourceXpathStr, filterXvrPOList(xvrPOList, 0, i + 1));
				} else {// 固定变量
					sourceXpathStr.append("{\"type:\":\"val\",\"value\":\"" + sourceXpaths[j] + "\"},");
				}
			}
			sourceXpathStr.append("{\"type\":\"slash\",\"value\":\"/\"},");
			sourceXpathStr.deleteCharAt(sourceXpathStr.length() - 1);
			sourceXpathStr.append("]");
			sourceXpathGridData.setXpathStr(sourceXpathStr.toString());
			sourceXpathGridDataList.add(sourceXpathGridData);
			// 目标目录
			if (isNeedTarget) {
				XpathAndFileNameVO targetXpathGridData = new XpathAndFileNameVO();
				StringBuffer targetXpathStr = new StringBuffer("[");
				targetXpathStr.append("{\"type\":\"slash\",\"value\":\"/\"},");// 前台从1开始展示，这边要多一个
				String[] targetXpaths = fddPOList.get(i).getTargetXpath().split("/");
				for (int j = 1; j < targetXpaths.length; j++) {
					targetXpathStr.append("{\"type\":\"slash\",\"value\":\"/\"},");
					if (targetXpaths[j].contains("${")) {// 有变量
						separateVariable(targetXpaths[j], targetXpathStr, filterXvrPOList(xvrPOList, 1, i + 1));
					} else {// 固定变量
						targetXpathStr.append("{\"type:\":\"val\",\"value\":\"" + targetXpaths[j] + "\"},");
					}
				}
				targetXpathStr.append("{\"type\":\"slash\",\"value\":\"/\"},");
				targetXpathStr.deleteCharAt(targetXpathStr.length() - 1);
				targetXpathStr.append("]");
				targetXpathGridData.setXpathStr(targetXpathStr.toString());
				// 判断是不是一样的（目标只要一个目录的时候）
				// 先简单判断，比较字符串长度，后续有更好的办法再说
				if (targetXpathGridDataList.size() == 0) {
					targetXpathGridDataList.add(targetXpathGridData);
				} else {
					if (targetXpathStr.length() != targetXpathGridDataList.get(0).getXpathStr().length()) {// 不一样
						targetXpathGridDataList.add(targetXpathGridData);
					}
				}
			}
		}
		record.setSourceXpathGridData(sourceXpathGridDataList);
		if (isNeedTarget) {
			record.setTargetXpathGridData(targetXpathGridDataList);
		}

		// 文件检索规则信息
		FileScanRuleInstanceArg fsriArg = new FileScanRuleInstanceArg();
		FileScanRuleInstanceCriteria fsriCriteria = fsriArg.createCriteria();
		fsriCriteria.andStateNotEqualTo(Status.CANCELLED.toString());
		fsriCriteria.andSourceDataIdEqualTo(dsdrPO.getSourceDataId());
		fsriArg.setOrderByClause("DIR_ORDER");
		List<FileScanRuleInstancePO> fsriPOList = fileScanRuleInstanceDao.selectByArg(fsriArg);
		logger.debug("文件检索规则信息：" + fsriPOList);
		record.setScanRange(fsriPOList.get(0).getScanRange());
		record.setFileType(fsriPOList.get(0).getFileType());
		record.setRuleDesc(fsriPOList.get(0).getRuleDesc());
		// 文件名配置规则
		List<XpathAndFileNameVO> fileNameGridDataList = new ArrayList<XpathAndFileNameVO>();
		for (int i = 0; i < fsriPOList.size(); i++) {
			XpathAndFileNameVO fileNameGridData = new XpathAndFileNameVO();
			fileNameGridData.setFileNameStr(fsriPOList.get(i).getFilenameScanXpath());
			fileNameGridData.setFileNameStr_OK(fsriPOList.get(i).getFilenameScanXpath());
			fileNameGridData.setDirOrder(fsriPOList.get(i).getDirOrder());
			fileNameGridDataList.add(fileNameGridData);
		}
		record.setFileNameGridData(fileNameGridDataList);
		logger.debug("整个策略信息：" + record);
		return record;
	}

	/**
	 * 过滤表达式变量映射信息
	 * 
	 * @param xvrPOList
	 * @param dataType
	 * @param dirOrder
	 * @return
	 */
	private List<XpathVariantRelPO> filterXvrPOList(List<XpathVariantRelPO> xvrPOList, int dataType, int dirOrder) {
		List<XpathVariantRelPO> result = new ArrayList<XpathVariantRelPO>();
		for (XpathVariantRelPO xvrPO : xvrPOList) {
			if (xvrPO.getDataType() == dataType && xvrPO.getDirOrder() == dirOrder) {
				result.add(xvrPO);
			}
		}
		return result;
	}

	/**
	 * 分离固定变量和可转换参数
	 * 
	 * @param value
	 * @param result
	 * @param xvrPOList
	 */
	private void separateVariable(String value, StringBuffer result, List<XpathVariantRelPO> xvrPOList) {
		int indexOfVariable_start = value.indexOf("${");
		int indexOfVariable_end = value.indexOf("}");
		String variableStr = "";
		String amountStr = "";
		if (indexOfVariable_start == 0) {// 前面没有固定变量了
			variableStr = value.substring(indexOfVariable_start + 2, indexOfVariable_end);
			for (XpathVariantRelPO xvrPO : xvrPOList) {
				if (xvrPO.getParamPriority().equals(new Integer(variableStr))) {
					if (xvrPO.getFuncId().intValue() == 4) {// 账期
						result.append("{\"type\":\"4\",\"value\":\"" + xvrPO.getVariantDefineValue() + "\"},");
					} else if (xvrPO.getFuncId().intValue() == 1 || xvrPO.getFuncId().intValue() == 5
							|| xvrPO.getFuncId().intValue() == 6 || xvrPO.getFuncId().intValue() == 7) {// 可转换变量
						result.append("{\"type\":\"list\",\"value\":\"" + xvrPO.getFuncId() + "\"},");
					} else if (xvrPO.getFuncId().intValue() == 2 || xvrPO.getFuncId().intValue() == 3) {// 时间变量
						result.append("{\"type\":\"timer\",\"value\":\"" + xvrPO.getFuncId() + ","
								+ xvrPO.getVariantDefineValue() + "\"},");
					} else if (xvrPO.getFuncId().intValue() == 0) {// 当前年元日
						result.append("{\"type\":\"nowDate\",\"value\":\"" + xvrPO.getFuncId() + ","
								+ xvrPO.getVariantDefineValue() + "\"},");
					}
				}
			}
			value = value.substring(indexOfVariable_end + 1, value.length());
		} else if (indexOfVariable_start == -1) {// 没有变量了
			result.append("{\"type:\":\"val\",\"value\":\"" + value + "\"},");
			value = "";
		} else {// 前面有固定变量
			amountStr = value.substring(0, indexOfVariable_start);
			result.append("{\"type:\":\"val\",\"value\":\"" + amountStr + "\"},");
			value = value.substring(indexOfVariable_start, value.length());
		}
		if (!"".equals(value)) {
			separateVariable(value, result, xvrPOList);
		}
	}

	@Override
	public DmFilePlanVO xpathVerify(DmFilePlanVO record) throws BaseAppException {
		Date nowDate = new Date();
		List<String> xpathList = new ArrayList<String>();
		List<XpathAndFileNameVO> sourceXpathGridData = JsonUtil.toList(record.getSourceXpathGridDataStr(),
				XpathAndFileNameVO.class);
		for (int i = 0; i < sourceXpathGridData.size(); i++) {
			List<String> xpathList_sub = new ArrayList<String>();
			List<TableNamePO> sourceXpathList = JsonUtil.toList(sourceXpathGridData.get(i).getXpathStr(),
					TableNamePO.class);
			List<String> valueList = new ArrayList<String>();
			for (TableNamePO tnPO : sourceXpathList) {
				if ("directory".equals(tnPO.getType())) {// 不需要保存
					continue;
				} else if ("slash".equals(tnPO.getType())) {
					if (xpathList_sub.size() == 0) {
						xpathList_sub.add("/");
					} else {
						for (int j = 0; j < xpathList_sub.size(); j++) {
							xpathList_sub.set(j, xpathList_sub.get(j) + "/");
						}
					}
					continue;
				} else if ("timer".equals(tnPO.getType())) {// 时间变量
					String[] timerStrs = tnPO.getValue().split(",");
					if ("2".equals(timerStrs[0])) {// 按年月->往前偏移
						valueList = exchangeFunc.yearMonthExchange(nowDate, Integer.parseInt(timerStrs[1]),
								Integer.parseInt(timerStrs[2]));
						setXpathValue(record, xpathList_sub, valueList, "{时间变量:按年月偏移出错}");
					} else if ("3".equals(timerStrs[0])) {// 按年月日->往前偏移
						valueList = exchangeFunc.dateExchange(nowDate, Integer.parseInt(timerStrs[1]),
								Integer.parseInt(timerStrs[2]));
						setXpathValue(record, xpathList_sub, valueList, "{时间变量:按年月日偏移出错}");
					}else if ("22".equals(timerStrs[0])) {// 按年月->往前偏移
						valueList = exchangeFunc.yearMonthWithSplitExchange(nowDate, Integer.parseInt(timerStrs[1]),
								Integer.parseInt(timerStrs[2]));
						setXpathValue(record, xpathList_sub, valueList, "{时间变量:按年-月偏移出错}");
					} else if ("33".equals(timerStrs[0])) {// 按年月日->往前偏移
						valueList = exchangeFunc.dateWithSplitExchange(nowDate, Integer.parseInt(timerStrs[1]),
								Integer.parseInt(timerStrs[2]));
						setXpathValue(record, xpathList_sub, valueList, "{时间变量:按年-月-日偏移出错}");
					}
					continue;
				} else if ("list".equals(tnPO.getType())) {// 可转换变量
					if ("1".equals(tnPO.getValue())) {// 按本地网
						valueList = exchangeFunc.latnIdExchange(record.getNodeId());
						setXpathValue(record, xpathList_sub, valueList, "{可转换变量:按本地网出错}");
					} else if ("5".equals(tnPO.getValue())) {// 按客户分组
						valueList = exchangeFunc.custCategoryExchange(record.getNodeId());
						setXpathValue(record, xpathList_sub, valueList, "{可转换变量:按客户分组出错}");
					} else if ("6".equals(tnPO.getValue())) {// 按清单序列
						valueList = exchangeFunc.detailSeqExchange(record.getNodeId());
						setXpathValue(record, xpathList_sub, valueList, "{可转换变量:按清单序列出错}");
					} else if ("7".equals(tnPO.getValue())) {// 按进程号分组
						valueList = exchangeFunc.pidExchange(record.getNodeId());
						setXpathValue(record, xpathList_sub, valueList, "{可转换变量:按进程号分组出错}");
					}
					continue;
				} else if ("4".equals(tnPO.getType())) {// 账期
					String[] timerStrs = tnPO.getValue().split(",");
					valueList = exchangeFunc.billingCycleExchange(nowDate, Integer.parseInt(timerStrs[0]),
							Integer.parseInt(timerStrs[1]));
					setXpathValue(record, xpathList_sub, valueList, "{可转换变量:按账期偏移出错}");
					continue;
				} else {
					if (xpathList_sub.size() == 0) {
						xpathList_sub.add(tnPO.getValue());
					} else {
						for (int j = 0; j < xpathList_sub.size(); j++) {
							xpathList_sub.set(j, xpathList_sub.get(j) + tnPO.getValue());
						}
					}
					continue;
				}
			}
			xpathList.addAll(xpathList_sub);
		}
		for (int i = 0; i < xpathList.size(); i++) {
			xpathList.set(i,
					("/" + xpathList.get(i) + "/").replaceAll("//", "/").replaceAll("//", "/").replaceAll("//", "/"));
		}
		record.setXpathList(xpathList);
		return record;
	}

	/**
	 * 设置目录的值
	 * 
	 * @param record
	 * @param xpathList
	 * @param valueList
	 * @param msg
	 */
	private void setXpathValue(DmFilePlanVO record, List<String> list, List<String> valueList, String msg) {
		if (list.size() == 0) {
			for (String value : valueList) {
				list.add(value);
			}
		} else {
			int xpathSize = list.size();
			for (int j = 0; j < xpathSize; xpathSize--) {
				if (valueList.size() != 0) {// 转换的值不为空
					for (String value : valueList) {
						list.add(list.get(j) + value);
					}
					list.remove(j);
				} else {
					list.add(list.get(j) + msg);
					list.remove(j);
					record.setXpathIsOK(false);
				}
			}
		}
	}

	@Override
	public DmFilePlanVO fileNameVerify(DmFilePlanVO record) throws BaseAppException {
		Date nowDate = new Date();
		String headStr = "-type f ";
		if("1".equals(record.getFileType())){// 目录
			headStr = "-type d ";
		}else if("0".equals(record.getFileType())){// 不限
			headStr = "";
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat sdfws = new SimpleDateFormat("yyyy-MM-dd");
		List<String> fileNameList = new ArrayList<String>();
		List<XpathAndFileNameVO> fileNameGridData = new ArrayList<XpathAndFileNameVO>();
		// 先过滤掉没有变动的规则
		List<Integer> dirOrders = new ArrayList<Integer>();
		// 再过滤掉删除的规则
		List<Integer> dirOrders_delete = new ArrayList<Integer>();
		List<XpathAndFileNameVO> fileNameGridData_original = JsonUtil.toList(record.getFileNameGridDataStr(),
				XpathAndFileNameVO.class);
		for (int i = 0; i < fileNameGridData_original.size(); i++) {
			if (fileNameGridData_original.get(i).getDirOrder() == 0) {// 新增的配置规则
				fileNameGridData.add(fileNameGridData_original.get(i));
				continue;
			} else if (fileNameGridData_original.get(i).getIsDelete() == 1) {// 删除的配置规则
				dirOrders_delete.add(fileNameGridData_original.get(i).getDirOrder());
				continue;
			} else {// 没有变动的规则
				dirOrders.add(fileNameGridData_original.get(i).getDirOrder());
			}
		}
		if ("edit".equals(record.getAct()) || "draft".equals(record.getAct()) || "saveDraft".equals(record.getAct())) {
			// 从后台找出原先的文件名信息
			FileScanRuleInstanceArg fsriArg = new FileScanRuleInstanceArg();
			FileScanRuleInstanceCriteria fsriCriteria = fsriArg.createCriteria();
			fsriCriteria.andStateNotEqualTo(Status.CANCELLED.toString());
			if (dirOrders.size() != 0) {
				fsriCriteria.andDirOrderIn(dirOrders);
			}
			if (dirOrders_delete.size() != 0) {
				fsriCriteria.andDirOrderNotIn(dirOrders_delete);
			}
			fsriCriteria.andSourceDataIdEqualTo(record.getSourceDataId());
			fsriArg.setOrderByClause("DIR_ORDER");
			List<FileScanRuleInstancePO> fsriPOList = fileScanRuleInstanceDao.selectByArg(fsriArg);

			// 表达式变量映射关系表
			XpathVariantRelArg xvrArg = new XpathVariantRelArg();
			XpathVariantRelCriteria xvrCriteria = xvrArg.createCriteria();
			xvrCriteria.andStateNotEqualTo(Status.CANCELLED.toString());
			xvrCriteria.andSourceDataIdEqualTo(record.getSourceDataId());
			if (dirOrders.size() != 0) {
				xvrCriteria.andDirOrderIn(dirOrders);
			}
			xvrCriteria.andDataTypeEqualTo(2);
			xvrArg.setOrderByClause("DIR_ORDER, PARAM_PRIORITY");
			List<XpathVariantRelPO> xvrPOList = xpathVariantRelDao.selectByArg(xvrArg);
			for (FileScanRuleInstancePO fsriPO : fsriPOList) {
				List<String> fileNameList_sub = new ArrayList<String>();
				List<String> valueList = new ArrayList<String>();
				// -type f 'name '${1}' -name '${2}' -name '${3}'
				String[] fileNames = fsriPO.getFilenameScanXpath().split("\\$\\{");
				// 查找出原来是什么文件类型，去除前面的字符串“-type f”之类的
				String fileNames_0 = fileNames[0];
				if(!"0".equals(fsriPO.getFileType())){// 文件或者目录
					fileNames_0 = fileNames_0.substring(8, fileNames_0.length());
				}
				// 再判断现在是什么文件类型
				if("1".equals(record.getFileType())){// 目录
					fileNames_0 = "-type d "+fileNames_0;
				}else if("2".equals(record.getFileType())){// 文件
					fileNames_0 = "-type f "+fileNames_0;
				}
				fileNameList_sub.add(fileNames_0);
				for (int i = 1; i < fileNames.length; i++) {
					// 1}' -name '
					String[] fileNames_sub = fileNames[i].split("\\}");
					for (XpathVariantRelPO xvrPO : xvrPOList) {
						if (xvrPO.getDirOrder() == fsriPO.getDirOrder()
								&& xvrPO.getParamPriority() == Integer.parseInt(fileNames_sub[0])) {
							if (xvrPO.getFuncId() == 0) {// 当前时间
								valueList.add(sdf.format(fsriPO.getStateDate()));
								setFileNameValue(record, fileNameList_sub, valueList, fileNames_sub[1], "{当前年月日:出错}");
							} else if (xvrPO.getFuncId() == 1) {// 本地网
								valueList = exchangeFunc.latnIdExchange(record.getNodeId());
								setFileNameValue(record, fileNameList_sub, valueList, fileNames_sub[1],
										"{可转换变量:按本地网出错}");
							} else if (xvrPO.getFuncId() == 2) {// 时间变量：年月
								String[] values = xvrPO.getVariantDefineValue().split(",");
								valueList = exchangeFunc.yearMonthExchange(nowDate, Integer.parseInt(values[0]),
										Integer.parseInt(values[1]));
								setFileNameValue(record, fileNameList_sub, valueList, fileNames_sub[1],
										"{时间变量:按年月偏移出错}");
							} else if (xvrPO.getFuncId() == 3) {// 时间变量：年月日
								String[] values = xvrPO.getVariantDefineValue().split(",");
								valueList = exchangeFunc.dateExchange(nowDate, Integer.parseInt(values[0]),
										Integer.parseInt(values[1]));
								setFileNameValue(record, fileNameList_sub, valueList, fileNames_sub[1],
										"{时间变量:按年月日偏移出错}");
							} else if (xvrPO.getFuncId() == 22) {// 时间变量：年-月
								String[] values = xvrPO.getVariantDefineValue().split(",");
								valueList = exchangeFunc.yearMonthWithSplitExchange(nowDate, Integer.parseInt(values[0]),
										Integer.parseInt(values[1]));
								setFileNameValue(record, fileNameList_sub, valueList, fileNames_sub[1],
										"{时间变量:按年月偏移出错}");
							} else if (xvrPO.getFuncId() == 33) {// 时间变量：年-月-日
								String[] values = xvrPO.getVariantDefineValue().split(",");
								valueList = exchangeFunc.dateWithSplitExchange(nowDate, Integer.parseInt(values[0]),
										Integer.parseInt(values[1]));
								setFileNameValue(record, fileNameList_sub, valueList, fileNames_sub[1],
										"{时间变量:按年月日偏移出错}");
							}else if (xvrPO.getFuncId() == 4) {// 账期
								String[] values = xvrPO.getVariantDefineValue().split(",");
								valueList = exchangeFunc.billingCycleExchange(nowDate, Integer.parseInt(values[0]),
										Integer.parseInt(values[1]));
								setFileNameValue(record, fileNameList_sub, valueList, fileNames_sub[1],
										"{可转换变量:按账期偏移出错}");
							} else if (xvrPO.getFuncId() == 5) {// 客户分组
								valueList = exchangeFunc.custCategoryExchange(record.getNodeId());
								setFileNameValue(record, fileNameList_sub, valueList, fileNames_sub[1],
										"{可转换变量:按客户分组出错}");
							} else if (xvrPO.getFuncId() == 6) {// 清单序列
								valueList = exchangeFunc.detailSeqExchange(record.getNodeId());
								setFileNameValue(record, fileNameList_sub, valueList, fileNames_sub[1],
										"{可转换变量:按清单序列出错}");
							} else if (xvrPO.getFuncId() == 7) {// 进程号分组
								valueList = exchangeFunc.pidExchange(record.getNodeId());
								setFileNameValue(record, fileNameList_sub, valueList, fileNames_sub[1],
										"{可转换变量:按进程号分组出错}");
							}else if (xvrPO.getFuncId() == 8) {// 当前时间 年-月-日
								valueList.add(sdfws.format(fsriPO.getStateDate()));
								setFileNameValue(record, fileNameList_sub, valueList, fileNames_sub[1], "{当前年-月-日:出错}");
							}
						}
					}
				}
				fileNameList_sub.add("-o");
				fileNameList.addAll(fileNameList_sub);
			}
		}

		// 对新增的文件名规则进行验证
		for (int i = 0; i < fileNameGridData.size(); i++) {
			List<String> fileNameList_sub = new ArrayList<String>();
			List<TableNamePO> tnPOList = JsonUtil.toList(fileNameGridData.get(i).getFileNameStr(), TableNamePO.class);
			List<String> valueList = new ArrayList<String>();
			for (TableNamePO tnPO : tnPOList) {
				if ("contain".equals(tnPO.getType())) {// 包含
					if (fileNameList_sub.size() == 0) {
						fileNameList_sub.add("' -name '");
					} else {
						for (int j = 0; j < fileNameList_sub.size(); j++) {
							fileNameList_sub.set(j, fileNameList_sub.get(j) + "' -name '");
						}
					}
					continue;
				} else if ("noContain".equals(tnPO.getType())) {// 不包含
					if (fileNameList_sub.size() == 0) {
						fileNameList_sub.add("' ! -name '");
					} else {
						for (int j = 0; j < fileNameList_sub.size(); j++) {
							fileNameList_sub.set(j, fileNameList_sub.get(j) + "' ! -name '");
						}
					}
					continue;
				} else if ("timer".equals(tnPO.getType())) {// 时间变量
					String[] timerStrs = tnPO.getValue().split(",");
					if ("2".equals(timerStrs[0])) {// 按年月->往前偏移
						valueList = exchangeFunc.yearMonthExchange(nowDate, Integer.parseInt(timerStrs[1]),
								Integer.parseInt(timerStrs[2]));
						setFileNameValue(record, fileNameList_sub, valueList, "{时间变量:按年月偏移出错}");
					} else if ("3".equals(timerStrs[0])) {// 按年月日->往前偏移
						valueList = exchangeFunc.dateExchange(nowDate, Integer.parseInt(timerStrs[1]),
								Integer.parseInt(timerStrs[2]));
						setFileNameValue(record, fileNameList_sub, valueList, "{时间变量:按年月日偏移出错}");
					} else if ("22".equals(timerStrs[0])) {// 按年-月->往前偏移
						valueList = exchangeFunc.yearMonthWithSplitExchange(nowDate, Integer.parseInt(timerStrs[1]),
								Integer.parseInt(timerStrs[2]));
						setFileNameValue(record, fileNameList_sub, valueList, "{时间变量:按年-月偏移出错}");
					} else if ("33".equals(timerStrs[0])) {// 按年-月-日->往前偏移
						valueList = exchangeFunc.dateWithSplitExchange(nowDate, Integer.parseInt(timerStrs[1]),
								Integer.parseInt(timerStrs[2]));
						setFileNameValue(record, fileNameList_sub, valueList, "{时间变量:按年-月-日偏移出错}");
					}

				} else if ("list".equals(tnPO.getType())) {// 可转换变量
					if ("1".equals(tnPO.getValue())) {// 按本地网
						valueList = exchangeFunc.latnIdExchange(record.getNodeId());
						setFileNameValue(record, fileNameList_sub, valueList, "{可转换变量:按本地网出错}");
					} else if ("5".equals(tnPO.getValue())) {// 按客户分组
						valueList = exchangeFunc.custCategoryExchange(record.getNodeId());
						setFileNameValue(record, fileNameList_sub, valueList, "{可转换变量:按客户分组出错}");
					} else if ("6".equals(tnPO.getValue())) {// 按清单序列
						valueList = exchangeFunc.detailSeqExchange(record.getNodeId());
						setFileNameValue(record, fileNameList_sub, valueList, "{可转换变量:按清单序列出错}");
					} else if ("7".equals(tnPO.getValue())) {// 按进程号分组
						valueList = exchangeFunc.pidExchange(record.getNodeId());
						setFileNameValue(record, fileNameList_sub, valueList, "{可转换变量:按进程号分组出错}");
					}
					continue;
				} else if ("4".equals(tnPO.getType())) {// 账期
					String[] timerStrs = tnPO.getValue().split(",");
					valueList = exchangeFunc.billingCycleExchange(nowDate, Integer.parseInt(timerStrs[0]),
							Integer.parseInt(timerStrs[1]));
					setFileNameValue(record, fileNameList_sub, valueList, "{可转换变量:按账期偏移出错}");
					continue;
				} else if ("nowDate".equals(tnPO.getType())) {// 当前年月日
					valueList.clear();
					valueList.add(sdf.format(new Date()));
					setFileNameValue(record, fileNameList_sub, valueList, "{当前年月日:出错}");
				} else if ("nowDateWithSplit".equals(tnPO.getType())) {// 当前年-月-日
					valueList.clear();
					valueList.add(sdfws.format(new Date()));
					setFileNameValue(record, fileNameList_sub, valueList, "{当前年-月-日:出错}");
				}else {// 固定变量
					if (fileNameList_sub.size() == 0) {
						fileNameList_sub.add(tnPO.getValue());
						record.setFileNameIsOK(false);
					} else {
						for (int j = 0; j < fileNameList_sub.size(); j++) {
							fileNameList_sub.set(j, fileNameList_sub.get(j) + tnPO.getValue());
						}
					}
				}
			}
			int subSize = fileNameList_sub.size();
			for (int j = 0; j < subSize; j++) {
				fileNameList_sub.set(j,
						headStr + fileNameList_sub.get(j).substring(2, fileNameList_sub.get(j).length()) + "'");
//				if("0".equals(record.getFileType())){
//					fileNameList_sub.add("-type d " + fileNameList_sub.get(j).substring(8, fileNameList_sub.get(j).length()) + "'");
//				}
			}
			fileNameList_sub.add("-o");
			fileNameList.addAll(fileNameList_sub);
		}
		record.setFileNameList(fileNameList);
		return record;
	}

	/**
	 * 设置文件名的值
	 * 
	 * @param record
	 * @param xpathList
	 * @param valueList
	 * @param msg
	 */
	private void setFileNameValue(DmFilePlanVO record, List<String> list, List<String> valueList, String msg) {
		if (list.size() == 0) {
			for (String value : valueList) {
				list.add(value);
			}
			record.setFileNameIsOK(false);
		} else {
			int xpathSize = list.size();
			for (int j = 0; j < xpathSize; xpathSize--) {
				if (valueList.size() != 0) {// 转换的值不为空
					for (String value : valueList) {
						list.add(list.get(j) + value);
					}
					list.remove(j);
				} else {
					list.add(list.get(j) + msg);
					list.remove(j);
					record.setFileNameIsOK(false);
				}
			}
		}
	}

	/**
	 * 设置文件名的值-从表里获取的数据
	 * 
	 * @param record
	 * @param xpathList
	 * @param valueList
	 * @param msg
	 */
	private void setFileNameValue(DmFilePlanVO record, List<String> list, List<String> valueList,
			String value_original, String msg) {
		if (list.size() == 0) {
			for (String value : valueList) {
				list.add(value + value_original);
			}
			record.setFileNameIsOK(false);
		} else {
			int xpathSize = list.size();
			for (int j = 0; j < xpathSize; xpathSize--) {
				if (valueList.size() != 0) {// 转换的值不为空
					for (String value : valueList) {
						list.add(list.get(j) + value + value_original);
					}
					list.remove(j);
				} else {
					list.add(list.get(j) + msg);
					list.remove(j);
					record.setFileNameIsOK(false);
				}
			}
		}
	}
}
